<template>
  <div>

    <template v-if="itemdata.sUITypeExp == 'normal'">

      <template >
        <input type="hidden" :id="index + '_' + itemdata.sField" v-model="itemdata.valuehidden" />
        <van-field :readonly="itemdata.sEditable==='false'" size="large" placeholder="请选择" :id="index + '_' + itemdata.sField + '_enum'" readonly
          v-model="itemdata.value" :label="itemdata.sTitle" @click="openEnum(itemdata)"
          :error-message="itemdata.errormsg" />
      </template>
      <!-- <template v-else>
        <van-cell :title="itemdata.sTitle">
          <template #default>
            <div v-html="itemdata.value"></div>
          </template>
        </van-cell>
      </template> -->
    </template>
    <template v-else-if="itemdata.sUITypeExp == 'multi'">
      <template>
        <input type="hidden" :id="index + '_' + itemdata.sField" v-model="itemdata.valuehidden" />
        <van-field :readonly="itemdata.sEditable==='false'"  size="large" placeholder="请选择" :id="index + '_' + itemdata.sField + '_enum'" readonly
          v-model="itemdata.value" :label="itemdata.sTitle" @click="openEnumMulti(itemdata)"
          :error-message="itemdata.errormsg" />
      </template>
      <!-- <template v-else>
        <van-cell :title="itemdata.sTitle">
          <template #default>
            <div v-html="itemdata.value"></div>
          </template>
        </van-cell>
      </template> -->
    </template>

    <van-action-sheet v-model="enumshow" :actions="actions" @select="selectedEnum" />
    <van-popup v-model="enumMultishow" round position="bottom">
      <van-checkbox-group v-model="selecteddata" ref="checkboxGroup">
        <van-cell v-for="(item, index) in enumMulti" :key="item.id" :title="item.name" @click="toggle(index)">
          <van-checkbox :name="item" ref="checkboxes" slot="right-icon" />
        </van-cell>
      </van-checkbox-group>
      <div style="text-align:right;padding-right:10vw;padding-top:2vh;padding-bottom:2vh;">
        <van-button type="info" size="small" @click="selectedEnumMulti">确认</van-button>
      </div>
    </van-popup>
  </div>
</template>
<script>
export default {
  data: function () {
    return {
      enumshow: false,
      enumMultishow: false,
      enumMulti: [],
      selecteddata: [],
      clickmodel: {},
      actions: [],
      x: []
    }
  },

  props: {
    jsondata: {
      type: Object,
      required: false
    },
    idobject: {
      type: String,
      required: false
    },
    itemdata: {
      type: Object,
      required: false
    },
    ishead: {
      type: Boolean,
      default: false
    },
    index: {
      type: Number,
      required: false
    }
  },
  watch: {
    'itemdata.value': {
      handler(newval) {
        this.$emit('handleFiledLink', { sField: this.itemdata.sField, value: newval, ishead: this.ishead, index: this.index })
      }
    }
  },
  methods: {
    selectedEnumMulti() {
      let enumid = ''
      let enumval = ''
      this.clickmodel.valuehidden = ''
      this.clickmodel.value = ''
      for (let i = 0; i < this.selecteddata.length; i++) {
        enumid += this.selecteddata[i].id + ';'
        enumval += this.selecteddata[i].name + ';'
      }
      this.clickmodel.valuehidden = enumid
      this.clickmodel.value = enumval
      this.clickmodel.errormsg = ''
      this.enumMultishow = false
    },
    selectedEnum(item) {
      this.clickmodel.valuehidden = item.id
      this.clickmodel.value = item.name
      this.clickmodel.errormsg = ''
      this.enumshow = false
      this.boolUpdate(this.clickmodel)
    },
    openEnum(datamodel) {
      if(datamodel.sEditable==='false'){
        return
      }
      this.actions = []
      this.clickmodel = datamodel
      let enumid = datamodel.allenumid
      let arrEnumid = enumid.split(';')
      let enumval = datamodel.allenumval
      let arrEnumval = enumval.split(';')
      for (let i = 0; i < arrEnumid.length; i++) {
        this.actions.push({ id: arrEnumid[i], name: arrEnumval[i] })
      }
      this.enumshow = true
    },
    openEnumMulti(datamodel) {
      if(datamodel.sEditable==='false'){
        return
      }
      this.enumMulti = []
      this.selecteddata = []
      this.clickmodel = datamodel
      let enumid = datamodel.allenumid
      let arrEnumid = enumid.split(';')
      let enumval = datamodel.allenumval
      let arrEnumval = enumval.split(';')
      for (let i = 0; i < arrEnumid.length; i++) {
        if (arrEnumid[i] === '') {
          continue
        }
        this.enumMulti.push({ id: arrEnumid[i], name: arrEnumval[i] })
      }
      this.enumMultishow = true
    },
    boolUpdate(clickmodel) {
      //console.log('boolUpdate22')
      let data = {}
      if (clickmodel.idObject === 'sorder' && clickmodel.sField === 'sfdj') {
        let sfdjval = clickmodel.value === '是' ? '带价格 ' : ''
        let fcval = ''
        data[this.idobject + '_id'] = this.jsondata[this.idobject + '_id']
        for (let groupindex in this.jsondata.head) {
          let itemgroup = this.jsondata.head[groupindex].groupvalue
          for (let itemindex in itemgroup) {
            if (itemgroup[itemindex].sField === 'fc') {
              fcval = itemgroup[itemindex].value
            }
          }
          for (let itemindex in itemgroup) {
            if (itemgroup[itemindex].sField === 'snote') {
              itemgroup[itemindex].value = sfdjval + fcval
            }
          }
        }
      }
      if (clickmodel.idObject === 'sorder' && clickmodel.sField === 'fc') {
        let sfdjval = ''
        let fcval = clickmodel.value
        data[this.idobject + '_id'] = this.jsondata[this.idobject + '_id']
        for (let groupindex in this.jsondata.head) {
          let itemgroup = this.jsondata.head[groupindex].groupvalue
          for (let itemindex in itemgroup) {
            if (itemgroup[itemindex].sField === 'sfdj') {
              sfdjval = itemgroup[itemindex].value === '是' ? '带价格 ' : ''
            }
          }
          for (let itemindex in itemgroup) {
            if (itemgroup[itemindex].sField === 'snote') {
              itemgroup[itemindex].value = sfdjval + fcval
            }
          }
        }
      }
    }
  }
}
</script>
